<main class="app-content">
    <div class="row">
        <div class="col-md-12">
            <div class="bs-component">
                <div class="card">
                    <h2 class="card-header">
                        <div class="form-check-inline">
                            {{ .task_info.TaskId }}
                        </div>
                    </h2>
                    <div class="card-body">
                        <b><span class="btn btn-info">任务名称</span></b>
                        <span class="btn btn-warning text-left">
                            {{ .task_info.TaskName }}
                        </span>
                        <br><br>
                        <b><span class="btn btn-info">任务状态</span></b>
                        <span class="btn btn-warning  text-left">{{ .task_info.State }}</span>
                        {{ if .task_info.ReceivedTime }}
                        <b><span class="btn btn-info">接收任务时间</span></b>
                        <span class="btn border-success text-left">
                           {{ .task_info.ReceivedTime }}</span>
                        {{ end }}
                        {{ if .task_info.StartedTime }}
                        <b><span class="btn btn-info">开始任务时间</span></b>
                        <span class="btn border-success text-left">
                           {{ .task_info.StartedTime }}</span>
                        {{ end }}
                        {{ if .task_info.KwArgs }}
                        <b><span class="btn btn-info">任务参数</span></b>
                        <br><br>
                        <span class="btn border-secondary text-left">
                            <div style="width:100%;white-space:normal;word-wrap:break-word;word-break:break-all;">
                            {{ .task_info.KwArgs }}
                            </div>
                        </span>
                        <br><br>
                        {{ end }}
                        {{ if .task_info.Result }}
                        <b><span class="btn btn-info">任务结果</span></b>
                        <span class="btn border-secondary text-left">
                            {{ .task_info.Result }}</span>
                        {{ end }}
                        {{ if .task_info.SucceededTime }}
                        <b><span class="btn btn-info">任务成功时间</span></b>
                        <span class="btn border-success text-left">
                           {{ .task_info.SucceededTime }}</span>
                        {{ end }}
                        {{ if .task_info.Worker }}
                        <b><span class="btn btn-info">子任务</span></b>
                        <span class="btn border-secondary text-left">
                            {{ .task_info.Worker }}</span>
                        {{ end }}
                        {{ if .task_info.Runtime }}
                        <b><span class="btn btn-info">任务执行时长</span></b>
                        <span class="btn border-success text-left">
                           {{ .task_info.Runtime }}</span>
                        <br><br>
                        {{ end }}
                        <b><span class="btn btn-info">工作空间</span></b>
                        <span class="btn border-success">{{ .task_info.Workspace }}</span>
                        <b><span class="btn btn-info">创建时间</span></b>
                        <span class="btn border-success">{{ .task_info.CreateTime }}</span>
                        <b><span class="btn btn-info">更新时间</span></b>
                        <span class="btn border-success">{{ .task_info.UpdateTime }}</span>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="tile">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th width="3%">序号</th>
                        <th width="5%">任务名称</th>
                        <th width="5%">任务状态</th>
                        <th width="20%">参数</th>
                        <th width="8%">结果</th>
                        <th width="8%">接收时间</th>
                        <th width="8%">启动时间</th>
                        <th width="6%">执行时长</th>
                        <th width="8%">worker</th>
                        <th width="6%">创建时间</th>
                        <th width="6%">更新时间</th>
                        <th width="6%">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {{ range .task_info.RunTaskInfo }}

                    <tr>
                        <td>{{ .Index }}</td>
                        <td>
                            <a href="/task-info-run?task_id={{ .TaskId }}" target="_blank">{{ .TaskName }}</a>
                        </td>
                        <td>
                            {{ .State }}
                            {{ if eq .State "CREATED" }}
                            <button class="btn btn-sm btn-danger" type="button" onclick="stop_task('{{ .TaskId }}')">
                                &nbsp;中止&nbsp;
                            </button>
                            {{ end }}
                        </td>
                        <td>
                            <div style="width:100%;white-space:normal;word-wrap:break-word;word-break:break-all;">
                                {{ .KwArgs }}
                            </div>
                        </td>
                        <td>
                            <div style="width:100%;white-space:normal;word-wrap:break-word;word-break:break-all;">
                                {{ if .ResultFile }}
                                <a href="{{ .ResultFile }}" target="_blank">{{ .Result }}</a>
                                {{ else }}
                                {{ .Result }}
                                {{ end }}
                            </div>
                        </td>
                        <td>{{ .ReceivedTime }}</td>
                        <td>{{ .StartedTime }}</td>
                        <td>{{ .Runtime }}</td>
                        <td>
                            <div style="width:100%;white-space:normal;word-wrap:break-word;word-break:break-all;">{{
                                .Worker }}
                            </div>
                        </td>
                        <td>{{ .CreateTime }}</td>
                        <td>{{ .UpdateTime }}</td>
                        <td><a class="btn btn-sm btn-danger" href="javascript:delete_task('{{ .Id }}')"
                               role="button" title="Delete"><i class="fa fa-trash-o"></i></a>
                        </td>
                    </tr>
                    {{ end }}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <!--row-->
</main>
<script src="static/js/jquery/jquery-3.3.1.min.js"></script>
<script src="static/js/bootstrap/popper.min.js"></script>
<script src="static/js/bootstrap/bootstrap.min.js"></script>
<script src="static/js/main.js"></script>
<script src="static/js/plugins/pace.min.js"></script>
<script src="static/js/sweetalert/sweetalert.min.js"></script>
<script>
    $(function () {
        $("title").html("{{ .task_info.TaskName }}-{{ .task_info.TaskId }}-taskinfo");
        //$('#btnsiderbar').click();
    });

    /**
     * 中止一个任务
     * @param task_id
     */
    function stop_task(task_id) {
        swal({
                title: "确定要中止任务?",
                text: "中止任务！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认中止",
                cancelButtonText: "取消",
                closeOnConfirm: true
            },
            function () {
                $.post("/task-stop-run",
                    {
                        "task_id": task_id,
                    }, function (data, e) {
                        if (e === "success") {
                            location.reload();
                        }
                    });
            });
    }

    /**
     * 删除一个任务
     * @param id
     */
    function delete_task(id) {
        swal({
                title: "确定要删除?",
                text: "该操作会删除当前任务，请确保当前任务已完成或中止！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认删除",
                cancelButtonText: "取消",
                closeOnConfirm: true
            },
            function () {
                $.post("/task-delete-run",
                    {
                        "id": id,
                    }, function (data, e) {
                        if (e === "success") {
                            location.reload();
                        }
                    });
            });
    }

</script>
